package edu.pku.sei.pgie.analyzer.core;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.preference.IPreferenceStore;

import edu.pku.sei.pgie.analyzer.internal.Metrics;
import edu.pku.sei.pgie.beans.CodeEntity;
import edu.pku.sei.pgie.beans.Link;
import edu.pku.sei.pgie.beans.Structure;
import edu.pku.sei.pgie.persistence.PersistenceException;
import edu.pku.sei.pgie.persistence.dao.DatabaseCollector;
import edu.pku.sei.pgie.persistence.graph.GraphCollector;
import edu.pku.sei.pgie.persistence.xml.XmlCollector;
import edu.pku.sei.pgie.ui.preferences.PreferenceConstants;

public class Context {
    IJavaProject javaProject;

    boolean enableOverrideAnalyze;

    boolean enableStructureAnalyze;

    boolean enableMethodAnalyze;

    String outputFormat;

    String outputDir;

    ICollector collector;

    String databaseDriver;

    String connectionString;

    String databasePassword;

    String databaseUserName;

    public String getConnectionString() {
		return connectionString;
	}

	public void setConnectionString(String connectionString) {
		this.connectionString = connectionString;
	}

	public String getDatabaseDriver() {
		return databaseDriver;
	}

	public void setDatabaseDriver(String databaseDriver) {
		this.databaseDriver = databaseDriver;
	}

	public String getDatabasePassword() {
		return databasePassword;
	}

	public void setDatabasePassword(String databasePassword) {
		this.databasePassword = databasePassword;
	}

	public String getDatabaseUserName() {
		return databaseUserName;
	}

	public void setDatabaseUserName(String databaseUserName) {
		this.databaseUserName = databaseUserName;
	}

	public Context() throws PersistenceException {
    	createCollector();
    }

	public Context(IPreferenceStore store) throws PersistenceException {
        setEnableOverrideAnalyze(store.getBoolean(PreferenceConstants.ENABLE_OVERRIDE_ANALYZE));
        setEnableStructureAnalyze(store.getBoolean(PreferenceConstants.ENABLE_STRUCTURE_ANALYZE));
        setEnableMethodAnalyze(store.getBoolean(PreferenceConstants.ENABLE_METHOD_ANALYZE));
        setOutputDir(store.getString(PreferenceConstants.OUPUT_DIR));
        setOutputFormat(store.getString(PreferenceConstants.OUTPUT_FORMAT));
        setDatabaseDriver(store.getString(PreferenceConstants.DATABASE_DRIVER_PREFERENCE));
        setDatabasePassword(store.getString(PreferenceConstants.DATABASE_PASSWORD_PREFERENCE));
        setDatabaseUserName(store.getString(PreferenceConstants.DATABASE_USER_PRERFERENCE));
        setConnectionString(store.getString(PreferenceConstants.DATABASE_URL_PREFERENCE));
		createCollector();
	}

    private void createCollector() throws PersistenceException {
        if("xml".equals(outputFormat)) {
            collector = new XmlCollector(this);
        } else if("graph".equals(outputFormat)) {
            collector = new GraphCollector(this);
        } else if("database".equals(outputFormat)) {
            collector = new DatabaseCollector(this);
        } else {
            // return a null collector
            collector = new ICollector(){
                public void collect(Structure structure) {}
                public void collect(Link link) {}
                public void collect(CodeEntity codeEntity) {}
                public void close() {}
                public void collect(Metrics metrics) {}
                public long nextCodeId() {
                    return 0;
                }
                public long nextStructureId() {
                    return 0;
                }
             };
        }
    }

    public String getOutputDir() {
        return outputDir;
    }

    public void setOutputDir(String outputDir) {
        this.outputDir = outputDir;
    }

    public String getOutputFormat() {
        return outputFormat;
    }

    public void setOutputFormat(String outputFormat) {
        this.outputFormat = outputFormat;
    }

    public boolean isEnableStructureAnalyze() {
        return enableStructureAnalyze;
    }

    public void setEnableStructureAnalyze(boolean enableStructureAnalyze) {
        this.enableStructureAnalyze = enableStructureAnalyze;
    }

    public boolean isEnableOverrideAnalyze() {
        return enableOverrideAnalyze;
    }

    public void setEnableOverrideAnalyze(boolean enableOverrideAnalyze) {
        this.enableOverrideAnalyze = enableOverrideAnalyze;
    }

    public IJavaProject getJavaProject() {
        return javaProject;
    }

    public void setJavaProject(IJavaProject javaProject) {
        this.javaProject = javaProject;
    }

    public ICollector getCollector() throws Exception {
        return collector;
    }

    public void dispose() {
    	if(collector != null) {
    		collector.close();
    	}
    }

    public boolean isEnableMethodAnalyze() {
        return enableMethodAnalyze;
    }

    public void setEnableMethodAnalyze(boolean enableMethodAnalyze) {
        this.enableMethodAnalyze = enableMethodAnalyze;
    }

}
